public class Offer_05_1 {
    /*
    请实现一个函数，把字符串 s 中的每个空格替换成"%20"。
    示例 1：
    输入：s = "We are happy."
    输出："We%20are%20happy."
   * */

    public static String replaceSpace(String s) {
        char[] chars = s.toCharArray();
        char[] newChars = new char[chars.length*3];
        int size = 0;
        for (char aChar : chars) {
            if(aChar == ' '){
                newChars[size++] = '%';
                newChars[size++] = '2';
                newChars[size++] = '0';
            }else{
                newChars[size++] = aChar;
            }
        }
        String str = new String(newChars,0,size);

        return str;
    }
    public static void main(String[] args) {
        System.out.println(replaceSpace("We are happy."));
    }
}
